//
//  FLLoadingView.m
//  Cat
//
//  Created by lushitong on 2019/1/31.
//  Copyright © 2019年 lushitong. All rights reserved.
//

#import "FLLoadingView.h"

@interface LoadingCircle : UIView

@property (nonatomic, assign) CGFloat progress;

@end

@implementation LoadingCircle

- (void)setProgress:(CGFloat)progress {
	_progress = progress;
	[self setNeedsDisplay];
}

- (instancetype)initWithFrame:(CGRect)frame {
	self = [super initWithFrame:frame];
	if (self) {
		self.backgroundColor = [UIColor clearColor];
		
		CALayer *maskLayer = [CALayer layer];
		maskLayer.contents = (id)[[UIImage imageNamed:@"angle-mask"] CGImage];
		maskLayer.frame = self.bounds;
		self.layer.mask = maskLayer;
	}
	return self;
}

- (void)drawRect:(CGRect)rect {
	
	CGContextRef context = UIGraphicsGetCurrentContext();
	
	CGContextSetStrokeColorWithColor(context, [UIColor lightGrayColor].CGColor);
	CGContextSetLineWidth(context, 2.0);
	
	CGFloat radius = MIN(self.width, self.height) / 2.0 - 3.0;
	
	static CGFloat startAngle = 3 * M_PI / 2.0;
	CGFloat endAngle = self.progress * (M_PI * 19 / 10) + startAngle;
	CGContextAddArc(context, self.width / 2, self.height / 2, radius, startAngle, endAngle, 0);
	
	CGContextDrawPath(context, kCGPathStroke);
}

@end

@interface FLLoadingView ()

@property (nonatomic, strong) LoadingCircle *circleView;
@property (nonatomic, strong) UIImageView *iconView;
@property (nonatomic, strong) UILabel *label;
@property (nonatomic, strong) NSTimer *timer;
@property (nonatomic, assign) CGFloat progress;
@property (nonatomic, strong) NSMutableArray *imgArray;
@property (nonatomic, strong) UIImageView *loadImgView;

@end

@implementation FLLoadingView

#pragma mark - Pub

- (void)showInView:(UIView *)view {
	[view addSubview:self];
	self.frame = view.bounds;
	
	self.loadImgView.animationImages = self.imgArray;
	self.loadImgView.animationRepeatCount = 0;
	self.loadImgView.animationDuration = (1 / 10.0) * self.imgArray.count;
	[self.loadImgView sizeToFit];
	self.loadImgView.center = CGPointMake(view.width / 2.0, (view.height - NavigationBarHeight) / 2.0 );
	[self.loadImgView startAnimating];
	
}

- (void)showInView:(UIView *)view updateViewyPoint:(CGFloat)yPoint {
	[self showInView:view];
	self.y = yPoint;
}

- (void)showInView:(UIView *)view updateViewyPoint:(CGFloat)yPoint centerViewyPoint:(CGFloat)circleViewYPoint {
	[self showInView:view];
	self.y = yPoint;
	self.loadImgView.y += circleViewYPoint;
}

- (void)dismiss {
	//    [self stopTimer];
	//    [self.circleView.layer removeAllAnimations];
	[self.loadImgView stopAnimating];
	[self removeFromSuperview];
}

- (void)loadingText:(NSString *)textDesc {
	self.label.hidden = !textDesc.length;
	self.label.text = textDesc;
	[self.label sizeToFit];
	self.label.center = CGPointMake(ScreenWidth / 2, self.loadImgView.bottom+kQKFitModuleDecimal(15)+self.label.height/2);
}

#pragma mark - Overrdie

- (instancetype)initWithFrame:(CGRect)frame {
	self = [super initWithFrame:frame];
	if (self) {
		[self loadSubviews];
		self.backgroundColor = COLOR_WITH_HEX(0xe6e6e6);
	}
	return self;
}

#pragma mark - Pri Method

- (void)loadSubviews {
	[self addSubview:self.loadImgView];
	[self addSubview:self.label];
}

- (void)startTimer {
	if (!self.timer) {
		self.timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(timerFilre:) userInfo:nil repeats:YES];
		[self.timer fire];
	}
}

- (void)stopTimer {
	[self.timer invalidate];
	self.timer = nil;
	self.progress = 0;
}

#pragma mark - Pri SEL

- (void)timerFilre:(id)sender {
	int random = arc4random() % 100;
	self.progress = self.progress + random * 1.0 / 100;
	self.circleView.progress = self.progress;
}

#pragma mark - Pri Property

- (UIImageView *)iconView {
	if (!_iconView) {
		_iconView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 35.0, 35.0)];
		_iconView.image = [UIImage imageNamed:@"refresh_header_icon"];
	}
	return _iconView;
}

- (UILabel *)label {
	if (!_label) {
		_label = [[UILabel alloc] init];
		_label.font = [UIFont fontWith3x:42.0];
		_label.textColor = COLOR_WITH_HEX(0xababab);
		_label.text = @"加载中...";
		_label.hidden = YES;
	}
	return _label;
}

- (LoadingCircle *)circleView {
	if (!_circleView) {
		_circleView = [[LoadingCircle alloc] initWithFrame:CGRectMake(0, 0, 35.0, 35.0)];
	}
	return _circleView;
}

- (NSMutableArray<UIImage *> *)imgArray {
	if (!_imgArray) {
		_imgArray = [[NSMutableArray alloc] initWithCapacity:0];
		for (int index = 0; index <= 20; index ++) {
			if (index < 10) {
				NSString *picName = [NSString stringWithFormat:@"中心加载_0000%d",index];
				UIImage *img = [UIImage imageNamed:[QKPodBundle getImagePath:picName fromBundle:@"QKBaseListViewLib"]];
				[_imgArray safeAddObject:img];
			} else {
				NSString *picName = [NSString stringWithFormat:@"中心加载_000%d",index];
				UIImage *img = [UIImage imageNamed:[QKPodBundle getImagePath:picName fromBundle:@"QKBaseListViewLib"]];
				[_imgArray safeAddObject:img];
			}
		}
	}
	return _imgArray;
}

- (UIImageView *)loadImgView {
	if (!_loadImgView) {
		_loadImgView = [[UIImageView alloc] init];
		_loadImgView.contentMode = UIViewContentModeScaleAspectFill;
		_loadImgView.image = self.imgArray.firstObject;
	}
	return _loadImgView;
}


@end
